import Vue from 'vue'
import store from '_store'
import VueRouter from 'vue-router'

// 导入自定义路由
import routes from './routes'

const TAHT = Vue.prototype

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
  TAHT.$loadingBar.start()
  if (to.meta.auth && !store.state.ticket) next('/login')
  if (to.meta.title) document.title = to.meta.title
  next()
})

// 路由后置守卫
router.afterEach((to, from) => {
  TAHT.$loadingBar.finish()
})

export default router
